var addmarkerWidget = LArk.widget.bindClass(LArk.widget.BaseWidget.extend({
    viewer: null,
    options: {
        resources: ["map.css"],
        view: {
            type: "window",
            url: "view.html",
            windowOptions: {
                width: 300,
                height: 400,
                position: {
                    top: 100,
                    right: 10
                }
            }
        }
    },
    drawControl: null,
    create: function () {
        this.drawControl = new LArk.Draw(this.viewer, {
            hasEdit: !1
        });
        var a = this;
        this.drawControl.on(LArk.draw.event.DrawCreated, function (t) {
            var e = t.entity;
            a.bindMarkerEx(e)
        }), this.drawControl.on(LArk.draw.event.EditMovePoint, function (t) {
            var e = t.entity;
            a.saveEntity(e)
        }), bindToLayerControl({
            pid: 0,
            name: "我的标记",
            visible: !0,
            onAdd: function () {
                a.drawControl.setVisible(!0)
            },
            onRemove: function () {
                a.drawControl.setVisible(!1)
            },
            onCenterAt: function (t) {
                var e = a.drawControl.getEntitys();
                a.viewer.flyTo(e, {
                    duration: t
                })
            }
        }), this.getList()
    },
    viewWindow: null,
    winCreateOK: function (t, e) {
        this.viewWindow = e
    },
    activate: function () {
        this.hasEdit(!0)
    },
    disable: function () {
        this.viewWindow = null, this.stopDraw(), this.hasEdit(!1)
    },
    stopDraw: function () {
        this.drawControl.stopDraw()
    },
    drawPoint: function () {
        this.stopDraw(), this.drawControl.startDraw({
            type: "billboard",
            style: {
                scale: 1,
                image: this.path + "img/marker.png"
            }
        })
    },
    editable: !1,
    hasEdit: function (t) {
        this.editable = t, this.drawControl.hasEdit(t)
    },
    bindMarkerEx: function (t) {
        if (null != t && null != t.position) {
            t.attribute.attr = t.attribute.attr || {}, t.attribute.attr.id = "0", 
            t.attribute.attr.name = "我的标记", 
            t.label = this.getLabelStyle(t.attribute.attr.name), 
            t.billboard.scaleByDistance = new Cesium.NearFarScalar(150, 1, 8e6, .2);
            var e = this;
            this.saveEntity(t, function () {
                t.popup = {
                    html: function (t) {
                        return e.getMarkerInhtml(t.attribute.attr)
                    },
                    anchor: [0, -35]
                }, e.viewer.LArkgis.popup.show(t, t.position._value)
            })
        }
    },
    saveEditFeature: function (t) {
        var e = this.drawControl.getEntityById(t);
        e.attribute.attr = e.attribute.attr || {},
         e.attribute.attr.name = $.trim($("#addmarker_attr_name").val()), 
         e.attribute.attr.remark = $.trim($("#addmarker_attr_remark").val()), 
         this.viewer.LArkgis.popup.close(), e.label.text = e.attribute.attr.name,
          this.saveEntity(e)
    },
    deleteEditFeature: function (t) {
        var e = this.drawControl.getEntityById(t);
        if (this.drawControl.deleteEntity(e), this.viewer.LArkgis.popup.close(), this.viewWindow.refMarkerList(), window.hasServer) sendAjax({
            url: "map/mark/" + t,
            type: "delete",
            success: function (t) {}
        });
        else {
            var a = this.getJsonData();
            haoutil.storage.add(this.storageName, a)
        }
    },
    getMarkerDataList: function () {
        for (var t = [], e = this.drawControl.getEntitys(), a = 0, r = e.length; a < r; a++) t.push(e[a].attribute.attr);
        return t
    },
    getMarkerInhtml: function (t) {
        return this.editable ? (t.name && "我的标记" != t.name || (t.name = ""), t.remark || (t.remark = ""), t.id || (t.id = "0"),
         '<div class="addmarker-popup-titile">添加标记</div><div class="addmarker-popup-content" ><form ><div class="form-group">  <label for="addmarker_attr_name" class="label_name">名称</label><input type="text" id="addmarker_attr_name" class="form-control" value="' + t.name + '" placeholder="请输入标记名称"    /> </div><div class="form-group">  <label  class="label_attr" for="addmarker_attr_remark">备注</label><textarea id="addmarker_attr_remark" class="form-control" rows="3" style="resize: none;" placeholder="请输入备注（可选填）"   >' + t.remark + '</textarea></div><div class="form-group" style="text-align: center;"><input type="button" class="btn btn-primary  btn-sm" value="保存" onclick="addmarkerWidget.saveEditFeature(' + t.id + ')" />&nbsp;&nbsp;<input type="button" class="btn btn-danger  btn-sm" value="删除" onclick="addmarkerWidget.deleteEditFeature(' + t.id + ')" /></div></form></div>') : '<div class="addmarker-popup-titile">我的标记</div><div class="addmarker-popup-content" ><form ><div class="form-group"><label>名称</label>：' + t.name + '</div><div class="form-group"><label>备注</label>：' + t.remark + "</div></form></div>"
    },
    centerAt: function (t) {
        var e = this.drawControl.getEntityById(t);
        if (e) {
            var a = e.position.getValue();
            a = LArk.point.addPositionsHeight(a, 2500), this.viewer.camera.flyTo({
                destination: a,
                duration: 3,
                orientation: {
                    heading: 0,
                    pitch: Cesium.Math.toRadians(-90),
                    roll: 0
                }
            })
        }
    },
    deleteAll: function () {
        this.drawControl.deleteAll(), this.viewWindow && this.viewWindow.refMarkerList(), window.hasServer ? sendAjax({
            url: "map/mark/delByUserId",
            data: JSON.stringify({
                userId: getUserId()
            }),
            type: "delete",
            dataType: "json",
            success: function (t) {}
        }) : haoutil.storage.del(this.storageName)
    },
    label_font_style: "normal small-caps normal 19px 楷体",
    getLabelStyle: function (t) {
        return new Cesium.LabelGraphics({
            text: "" == t ? "我的标记" : t,
            font: this.label_font_style,
            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
            outlineColor: Cesium.Color.BLACK,
            outlineWidth: 3,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(0, -50),
            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0, 2e5)
        })
    },
    getJsonData: function () {
        for (var t = [], e = this.drawControl.getEntitys(), a = 0, r = e.length; a < r; a++) {
            var i = e[a],
                n = i.attribute.attr,
                s = LArk.draw.attr.billboard.getCoordinates(i),
                o = {
                    id: n.id,
                    name: n.name,
                    remark: n.remark,
                    x: s[0][0],
                    y: s[0][1],
                    z: s[0][2]
                };
            t.push(o)
        }
        return JSON.stringify(t)
    },
    jsonToLayer: function (t, e) {
        if (null != t && 0 != t.length) {
            e && this.drawControl.deleteAll();
            for (var a = this, r = [], i = 0; i < t.length; i++) {
                var n = t[i];
                if (n.x && n.y) {
                    var s = {
                        type: "billboard",
                        attr: {
                            id: n.id || "",
                            name: n.name || "",
                            remark: n.remark || ""
                        },
                        style: {
                            scale: 1,
                            image: this.path + "img/marker.png"
                        }
                    };
                    if (!e) {
                        var o = this.drawControl.getEntityById(s.attr.id);
                        this.drawControl.deleteEntity(o)
                    }
                    var d = Cesium.Cartesian3.fromDegrees(n.x, n.y, n.z || 0);
                    (o = this.drawControl.attributeToEntity(s, d)).popup = {
                        html: function (t) {
                            return a.getMarkerInhtml(t.attribute.attr)
                        },
                        anchor: [0, -35]
                    }, o.billboard.scaleByDistance = new Cesium.NearFarScalar(150, 1, 8e6, .2), o.label = this.getLabelStyle(s.attr.name), r.push(o)
                }
            }
            this.viewer.flyTo(r, {
                duration: 2
            }), this.viewWindow && this.viewWindow.refMarkerList()
        }
    },
    storageName: "LArk_addmarker",
    getList: function () {
        var e = this;
        if (window.hasServer) {
            e = this;
            sendAjax({
                url: "map/mark/list",
                type: "get",
                success: function (t) {
                    e.jsonToLayer(t, !0)
                }
            })
        } else {
            var t = haoutil.storage.get(this.storageName);
            if (null != t && "null" != t) {
                var a = JSON.parse(t);
                this.jsonToLayer(a, !0)
            }
        }
    },
    saveEntity: function (e, a) {
        if (window.hasServer) {
            var t = e.attribute.attr,
                r = LArk.draw.attr.billboard.getCoordinates(e),
                i = this;
            "0" == t.id ? sendAjax({
                url: "map/mark/add",
                data: JSON.stringify({
                    name: t.name,
                    remark: t.remark,
                    x: r[0][0],
                    y: r[0][1],
                    z: r[0][2],
                    userId: getUserId()
                }),
                type: "post",
                contentType: "application/json",
                success: function (t) {
                    e.attribute.attr.id = t, a && a(), i.viewWindow.refMarkerList()
                }
            }) : sendAjax({
                url: "map/mark/update",
                data: JSON.stringify({
                    id: t.id,
                    name: t.name,
                    remark: t.remark,
                    x: r[0][0],
                    y: r[0][1],
                    z: r[0][2],
                    userId: getUserId()
                }),
                type: "post",
                contentType: "application/json",
                success: function (t) {
                    a && a(), i.viewWindow.refMarkerList()
                }
            })
        } else {
            "0" == e.attribute.attr.id && (e.attribute.attr.id = (new Date).format("yyyyMMddHHmmss"));
            var n = this.getJsonData();
            haoutil.storage.add(this.storageName, n), a && a(), this.viewWindow.refMarkerList()
        }
    }
}));